package export;

import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRTextElement;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.engine.util.JRProperties;

class ExcelExporter extends AbstractExporter {

    public ExcelExporter() {
        super();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void configExporter(ExportRequest request) {
        if (request.getAdditionalProperty(ReportPropertyConstants.SHEET_NAMES, String[].class) != null) {
            exporter.setParameter(JRXlsExporterParameter.SHEET_NAMES,
                    request.getAdditionalProperty(ReportPropertyConstants.SHEET_NAMES, String[].class));
        }
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void initialize() {
        exporter = initializeJRExporter();

        exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
        exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS, Boolean.FALSE);
        exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
        exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
        exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS, Boolean.FALSE);
        exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
        exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE);
        exporter.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.FALSE);
        JRProperties.setProperty(JRTextElement.PROPERTY_PRINT_KEEP_FULL_TEXT, true);
        

        // suppress page headers and footers when exporting to XLS
        JRProperties.setProperty(CWSJRXlsExporterParameter.PROPERTY_EXPORT_EXCLUDE_ORIGIN_BAND_1,
                CWSJRXlsExporterParameter.PAGE_HEADER);
        JRProperties.setProperty(CWSJRXlsExporterParameter.PROPERTY_EXPORT_EXCLUDE_ORIGIN_BAND_2,
                CWSJRXlsExporterParameter.PAGE_FOOTER);

        // this gets rid of the repeating column headers and footers in excel exports
        JRProperties.setProperty(CWSJRXlsExporterParameter.PROPERTY_EXPORT_XLS_EXCLUDE_ORIGIN_BAND_1,
                CWSJRXlsExporterParameter.COLUMN_HEADER);
        JRProperties.setProperty(CWSJRXlsExporterParameter.PROPERTY_EXPORT_XLS_EXCLUDE_ORIGIN_BAND_2,
                CWSJRXlsExporterParameter.PAGE_FOOTER);

        // keep the first column header on export
        JRProperties.setProperty(CWSJRXlsExporterParameter.PROPERTY_EXPORT_XLS_EXCLUDE_ORIGIN_KEEP_FIRST_BAND_1,
                CWSJRXlsExporterParameter.PAGE_HEADER);
        JRProperties.setProperty(CWSJRXlsExporterParameter.PROPERTY_EXPORT_XLS_EXCLUDE_ORIGIN_KEEP_FIRST_BAND_2,
                CWSJRXlsExporterParameter.COLUMN_HEADER);
    }
    
    protected JRExporter initializeJRExporter(){
    	return new JRXlsxExporter();
    }
}
